package org.wlw.teacher.config;

import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.apache.ibatis.session.Configuration;

import javax.sql.DataSource;

public class MybatisConfig {
    //完成了的mybatis的配置
    @Bean
    public SqlSessionFactoryBean createSqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(dataSource);
        fb.setTypeAliasesPackage("org.wlw.teacher.models");

        //打开日志
        Configuration cfg = new Configuration();
        // 可选：下划线转驼峰
        // cfg.setMapUnderscoreToCamelCase(true);
        // 关键：让 MyBatis 通过 SLF4J 打日志
        cfg.setLogImpl(StdOutImpl.class);
        fb.setConfiguration(cfg);
        return fb;
    }

    //会自动创建dao对象，此时service可以直接进行注入
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("org.wlw.teacher.mapper");
        return msc;
    }
}
